package com.zl.easybuy.service.impl;

import com.zl.easybuy.mapper.UserMapper;
import com.zl.easybuy.pojo.User;
import com.zl.easybuy.service.UserService;
import com.zl.easybuy.util.PasswordUtils;
import com.zl.easybuy.util.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    //注入UserMapper对象
    @Autowired
    private UserMapper userMapper;


    @Override
    public boolean addUser(User user) {
        if (userMapper.queryByLoginName(user.getLoginName())==null){
            user.setPassword(PasswordUtils.generate(user.getPassword()));
            if(userMapper.addUser(user)>0){
                return true;
            }
        }
        return false;
    }

    @Override
    public User queryById(Integer id) {
        User user = userMapper.queryById(id);
        return user;
    }

    @Override
    public boolean login(String loginName, String password) {
        User user = userMapper.queryByLoginName(loginName);
        if (user!=null){
            if (PasswordUtils.verify(password,user.getPassword())){
                SessionUtils.setLoginUserInSession(user);
                return true;
            }
        }
        return false;
    }
}
